import ViewMgr from '../manager/ViewMgr';
import { UICommandKey } from '../define/UICommand';
import DragonNode from './DragonNode';
import { Hero } from '../object/Hero';
import { DragonManager } from './DragonManager';
import TableMgr from '../manager/TableMgr';
import { AnimationKey } from '../define/BattleDefine';

const { ccclass, property } = cc._decorator;

@ccclass
export default class DragonBox extends cc.Component {
  public _model: DragonNode;
  private _battleObj: Hero;
  private _mod: number = 0;
  private _modelWidth: number;
  private _modelHeight: number;
  private _beatBoxLoc: cc.Vec2;

  @property(cc.Node)
  box: cc.Node = null;
  // LIFE-CYCLE CALLBACKS:

  // onLoad () {}

  start() {
    //ViewMgr.ins.addEventListener(UICommandKey.onBattleHeroReady,this.recycle,this);
  }
  public refresh(battleObj: Hero) {
    if (this._mod == battleObj.view.mod) return;
    this.clearHero();
    this._battleObj = battleObj;
    this._mod = this._battleObj.view.mod;

    var conf: res_dragonNode = TableMgr.inst.getDragonConfigById(this._mod);
    this._model = DragonManager.getModel(conf.path, conf.baseBone);
    this._model.playAnimation(AnimationKey.stand, 0);
    this.box.addChild(this._model);
    this._modelWidth = conf.width;
    this._modelHeight = conf.height;
    this.box.width = this._modelWidth;
    this.box.height = this._modelHeight;
  }

  public clearHero(): void {
    if (this._model && this._model.parent) {
      console.log('清除上次人物模型', this._model.resUrl);
      this._model.color = new cc.Color(255, 255, 255);
      this._model.stopAnimation();
      this.box.removeChild(this._model);
      DragonManager.resetNode(this._model);
      this._model.targetOff(this._model);
      DragonManager.putModel(this._model);
      this._model = null;
      this._mod = 0;
    }
  }

  public setScaleX(value: number) {
    this._model.scaleX = value;
  }
  public getBeatPointToWorld(): cc.Vec2 {
    return this.box.convertToWorldSpaceAR(this._beatBoxLoc);
  }
  public getModel(): DragonNode {
    return this._model;
  }
  public get model(): DragonNode {
    return this._model;
  }
  public get battleObj(): Hero {
    return this._battleObj;
  }
  public addEffects(eff: DragonNode, boxType: number) {
    var loc;
    switch (boxType) {
      default:
        loc = cc.v2(0, 0);
        break;
    }
    eff.position = loc;
    eff.parent = this.box;
  }
  public removeEffects(eff) {
    eff.parent = null;
  }
  onDestroy() {
    if (this._model && this._model.parent) {
      this.box.removeChild(this._model);
      DragonManager.putModel(this._model);
    }
  }
  private recycle(): void {
    if (this._model && this._model.parent) {
      console.log('回收人物模型', this._model.resUrl);
      this._model.color = new cc.Color(255, 255, 255);
      this._model.stopAnimation();
      this.box.removeChild(this._model);
      DragonManager.resetNode(this._model);
      this._model.targetOff(this._model);
      DragonManager.putModel(this._model);
      this._model = null;
      this._mod = 0;
    }
  }

  // update (dt) {}
}
